package nsu.grandfinal;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;

public class UserSetting_ extends PreferenceActivity {
	private DatabaseHandle	dbHandle;
	private double					laitude;
	private double					longitude;
	private int							ID;
	private int							mode;
	private int							range;
	private int							hour;
	private int							minute;
	private int							day;
	private int							alaramcode;
	private String					address;
	private String					featureName;
	private long						info;
	private Calendar				calendar;
	
	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		day = 0;
		int LD;
		calendar = Calendar.getInstance();
		dbHandle = new DatabaseHandle(this).open();
		Intent getIntent = getIntent();
		range = 50;
		mode = 1;
		if (getIntent.getStringExtra("sender") != null) {
			if (dbHandle.consCursor.getCount() > 0) LD = dbHandle.getLastId();
			else LD = 0;
			ID = getIntent.getIntExtra("id", LD + 1);
			if (getIntent.getStringExtra("sender").equals("saveList")) {
				info = getIntent.getLongExtra("infoID", 0);
				range = getIntent.getIntExtra("range", 50);
				alaramcode = dbHandle.getIntData(ID, "alarmcode");
				hour = dbHandle.getIntData(ID, "hour");
				minute = dbHandle.getIntData(ID, "minute");
				day = dbHandle.getIntData(ID, "day");
				mode = dbHandle.getIntData(ID, "mode");
			}
			featureName = getIntent.getStringExtra("featureName");
			address = getIntent.getStringExtra("address");
			laitude = getIntent.getDoubleExtra("Lat", 0);
			longitude = getIntent.getDoubleExtra("Lon", 0);
			Log.i("Lat:", laitude + "");
			Log.i("Lon:", longitude + "");
			if (day == 0) {
				day = calendar.get(Calendar.DAY_OF_WEEK);
			}
		}
		String sound = "";
		if (mode == 0) {
			sound = "벨소리";
		} else if (mode == 1) {
			sound = "진동";
		} else if (mode == 2) {
			sound = "무음";
		}
		String days = "";
		if (day == 0) {
			days = "일";
		} else if (day == 2) {
			days = "월";
		} else if (day == 3) {
			days = "화";
		} else if (day == 4) {
			days = "수";
		} else if (day == 5) {
			days = "목";
		} else if (day == 6) {
			days = "금";
		} else if (day == 7) {
			days = "토";
		} else if (day == 1) {
			days = "일";
		}
		
		SharedPreferences prefs = getSharedPreferences(String.valueOf(ID), MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("preName", featureName);
		editor.putString("preRange", String.valueOf(range));
		editor.putString("preTime", hour + ":" + minute);
		editor.putString("preMode", sound);
		editor.putString("preDays", days);
		editor.commit();
		
		prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
			Preference	preName		= findPreference("prefName");
			Preference	preRange	= findPreference("prefRange");
			Preference	preDays		= findPreference("prefDays");
			Preference	preTime		= findPreference("prefTime");
			Preference	preMode		= findPreference("prefMode");
			
			@SuppressLint("NewApi")
			public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
				Log.i("", "변경");
				preName.setSummary(prefs.getString("prefName", ""));
				preRange.setSummary(prefs.getString("prefRange", ""));
				preTime.setSummary(prefs.getString("prefTime", ""));
				preMode.setSummary(prefs.getString("prefMode", ""));
				Set<String> set = prefs.getStringSet("prefDays", new HashSet<String>());
				preDays.setSummary(set.toString());
			}
		});
	}
	
	@SuppressLint("NewApi")
	@Override
	protected void onPause() {
		super.onPause();
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		
		Set<String> set = prefs.getStringSet("prefDays", new HashSet<String>());
		Log.i("", prefs.getString("prefName", ""));
		Log.i("", prefs.getString("prefRange", ""));
		Log.i("", prefs.getString("prefTime", ""));
		Log.i("", prefs.getString("prefMode", ""));
		Log.i("", set.toString());
		// if (alaramcode == 0) alaramcode = (int) (Math.random() * 1000000);
		// address = tvAddress.getText().toString();
		// featureName = teName.getText().toString();
		// range = getRadioButton("range");
		// mode = getRadioButton("mode");
		// hour = calendar.get(Calendar.HOUR_OF_DAY);
		// minute = calendar.get(Calendar.MINUTE);
		// day = getRadioButton("day");
		// dbHandle = new DatabaseHandle(UserSetting.this);
		// dbHandle.open();
		// if (dbHandle.consCursor.getCount() == 0) dbHandle.createNote(address, featureName, range, laitude, longitude, mode, hour, minute, day,
		// alaramcode);
		// else if (ID == info) dbHandle.updateNote(ID, address, featureName, range, laitude, longitude, mode, hour, minute, day, alaramcode);
		// else dbHandle.createNote(address, featureName, range, laitude, longitude, mode, hour, minute, day, alaramcode);
		// dbHandle.close();
		// Intent sendIntent = new Intent(UserSetting.this, AlarmReceive.class);
		// sendIntent.putExtra("id", ID);
		// sendIntent.putExtra("featureName", featureName);
		// sendIntent.putExtra("address", address);
		// sendIntent.putExtra("Lat", laitude);
		// sendIntent.putExtra("Lon", longitude);
		// sendIntent.putExtra("mode", mode);
		// sendIntent.putExtra("range", range);
		// sendIntent.putExtra("day", day);
		// sendIntent.putExtra("alarmcode", alaramcode);
		// sendIntent.putExtra("sender", "UserSetting");
		// PendingIntent pender = PendingIntent.getBroadcast(UserSetting.this, alaramcode, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		// AlarmManager alarm = (AlarmManager) UserSetting.this.getSystemService(Context.ALARM_SERVICE);
		// alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pender);
		finish();
		dbHandle.close();
	}
}